package stat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.util.TreeMap;

public class DegreeDis {
	
	private static TreeMap<Integer, Integer> nodeDeg = new TreeMap<Integer, Integer>();
	private static TreeMap<Integer, Integer> nodeWeg = new TreeMap<Integer, Integer>();
	
	private static void process(String str) {
		String[] tokens = str.split("\t");
		Integer nodeId = Integer.parseInt(tokens[0]);
		Integer edgeWeight = Integer.parseInt(tokens[2]);
		
		Integer degree = nodeDeg.get(nodeId);
		Integer weight = nodeWeg.get(nodeId);
		
		nodeDeg.put(nodeId, degree == null ? 1 : degree + 1);
		nodeWeg.put(nodeId, weight == null ? edgeWeight : weight + edgeWeight);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String dataPath = "/home/ddxu/data/newEdges4activeUsers2.txt";
		String resultPath1 = "/home/ddxu/data/newNodeDegree2.txt";
		String resultPath2 = "/home/ddxu/data/newNodeWeight2.txt";
		
		//int flag = Integer.parseInt(args[0]);
		
		String str;
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(dataPath));
			while((str = in.readLine()) != null) {
				process(str);
			}
			in.close();
			
			BufferedWriter out = new BufferedWriter(new FileWriter(resultPath1));
			for(Integer key : nodeDeg.keySet()) {
				out.write(key.toString());
				out.write("\t");
				out.write(nodeDeg.get(key).toString());
				out.newLine();
			}
			out.flush();
			out.close();
			
			out = new BufferedWriter(new FileWriter(resultPath2));
			for(Integer key : nodeWeg.keySet()) {
				out.write(key.toString());
				out.write("\t");
				out.write(nodeWeg.get(key).toString());
				out.newLine();
			}
			out.flush();
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
